package com.joker;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static com.joker.ZookeeperConstant.ZK_ADDRESS;

public class ZooKeeperCRUDExample {
    private ZooKeeper zk;
    private static final int SESSION_TIMEOUT = 5000;
    private CountDownLatch connectedSignal = new CountDownLatch(1);

    // 连接ZooKeeper
    public void connect() throws IOException, InterruptedException {
        zk = new ZooKeeper(ZK_ADDRESS, SESSION_TIMEOUT, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    connectedSignal.countDown();
                }
            }
        });
        connectedSignal.await();
        System.out.println("连接成功!");
    }

    // 关闭连接
    public void close() throws InterruptedException {
        if (zk != null) {
            zk.close();
        }
    }

    // 创建节点
    public String createNode(String path, byte[] data) throws KeeperException, InterruptedException {
        return zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    // 创建临时节点
    public String createEphemeralNode(String path, byte[] data) throws KeeperException, InterruptedException {
        return zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    }

    // 检查节点是否存在
    public Stat exists(String path) throws KeeperException, InterruptedException {
        return zk.exists(path, false);
    }

    // 获取节点数据
    public String getData(String path) throws KeeperException, InterruptedException {
        byte[] data = zk.getData(path, false, null);
        return new String(data);
    }

    // 设置节点数据
    public Stat setData(String path, byte[] data) throws KeeperException, InterruptedException {
        return zk.setData(path, data, -1);
    }

    // 获取子节点列表
    public List<String> getChildren(String path) throws KeeperException, InterruptedException {
        return zk.getChildren(path, false);
    }

    // 删除节点
    public void deleteNode(String path) throws KeeperException, InterruptedException {
        zk.delete(path, -1);
    }

    // 使用示例
    public static void main(String[] args) {
        ZooKeeperCRUDExample example = new ZooKeeperCRUDExample();
        
        try {
            // 连接
            example.connect();
            
            // 创建持久节点
            String nodePath = example.createNode("/test-node", "Hello ZooKeeper".getBytes());
            System.out.println("创建节点: " + nodePath);
            
            // 检查节点是否存在
            Stat stat = example.exists("/test-node");
            System.out.println("节点存在: " + (stat != null));
            
            // 获取节点数据
            String data = example.getData("/test-node");
            System.out.println("节点数据: " + data);
            
            // 更新节点数据
            example.setData("/test-node", "Updated data".getBytes());
            System.out.println("更新后数据: " + example.getData("/test-node"));
            
            // 创建子节点
            example.createNode("/test-node/child1", "Child 1 data".getBytes());
            example.createNode("/test-node/child2", "Child 2 data".getBytes());
            
            // 获取子节点列表
            List<String> children = example.getChildren("/test-node");
            System.out.println("子节点: " + children);
            
            // 删除节点
            example.deleteNode("/test-node/child1");
            example.deleteNode("/test-node/child2");
            example.deleteNode("/test-node");
            System.out.println("节点已删除");
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                example.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}